package francis.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import francis.model.VideoInfo;
import francis.utils.VideoUtil;
import francis.utils.WebTools;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;


/**
 * 
 * @author francis started at 2011-11-18
 * 
 */
public class ClassifyActivity extends Activity {
	
	private Map<String,String> classify_map = new HashMap<String,String>();
	//add by hogan 2011-11-24
	private List<Map<String,Object>> show_info = new ArrayList<Map<String, Object>>();
	private SimpleAdapter adapter;
	private boolean isClassifyMode = true;    //是否是某个分类页面
	private boolean isClassifyIndex = true;    //是否已经是分类的首页
	private ListView classify_view;
	private List<VideoInfo> vInfos;
	//end
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.classify);
		
		classify_view = (ListView)this.findViewById(R.id.classify_list);
		
		loadOrignalData();
		
		//ListView点击响应
		classify_view.setOnItemClickListener(new OnItemClickListener() 
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) 
			{		    
			    if (isClassifyMode)    //显示某个分类页面
			    {			    	
			    	Map<String, Object> singleUrlMap = show_info.get(position);
			    	//清除原来的类别信息之前，获取点击的item的map
			    	show_info.clear();
			    	//从map中获取他的URL，并获取URL的网页信息
				    Object URL = singleUrlMap.get("url");
			    	vInfos = VideoUtil.getVideoInfos(URL.toString());
			    	Bitmap bitmap;
			    	//将新的URL内容赋值给ListArrary，ListArrary在上面已经清空
			    	for(VideoInfo info : vInfos) 
			    	{	
						bitmap = WebTools.returnBitMap(info.getPic());
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("url", bitmap);
						map.put("title",info.getTitle());
						map.put("tag", info.getAuthor() + " " + info.getCreate());
						show_info.add(map);
					}
			    	//重新生成适配器，布局文件为list_item
			    	adapter = new SimpleAdapter(ClassifyActivity.this,show_info,R.layout.list_item,
							new String[]{"url","title","tag"},
							new int[]{R.id.list_img,R.id.list_title,R.id.list_tag});
					
					adapter.setViewBinder(new ViewBinder() {
						@Override
						public boolean setViewValue(View view, Object data,
								String textRepresentation) {
							
							if(view instanceof ImageView && data instanceof Bitmap) {
								ImageView iv = (ImageView)view;
								iv.setImageBitmap((Bitmap)data);
								return true;
							}
							return false;
						}
					});	
					////设置ListView的adapter
					classify_view.setAdapter(adapter);
					//isClassifyMode取反，表示下次点击显示详细信息
					isClassifyMode = !isClassifyMode;
					isClassifyIndex = false;
				}
			    else      //显示详细信息，启动VideoDetailActivity
			    {
					VideoInfo info = vInfos.get(position);
					Intent intent = new Intent();
					intent.putExtra("aid", info.getAid());
					intent.setClass(ClassifyActivity.this, VideoDetailActivity.class);
					startActivity(intent);
				}
			}
		});
	}
	
	/**
	 * 载入分类首页的原始数据
	 */
	private void loadOrignalData()
	{
		// 载入类别信息
		this.loadClassify();
		//将类别信息加入到ListArrary中
		if( !show_info.isEmpty())
		{
			show_info.clear();
		}
		
		for(String name : classify_map.keySet()) 
		{
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("header_text", name);
			map.put("url",classify_map.get(name));
			show_info.add(map);
		}
		//以ListArrary为内容，产生adapter，布局文件为classify_item
		adapter = new SimpleAdapter(this,show_info,R.layout.classify_item,
				new String[]{"header_text"},
				new int[]{R.id.clasify_item_text});
		//设置ListView的adapter
		classify_view.setAdapter(adapter);
	}
	
	private void loadClassify() {
		// TODO 重构，从xml载入
		if(classify_map.isEmpty()) {
			classify_map.put("动画", "http://api.bilibili.tv/list?type=xml&tid=1");
			classify_map.put("音乐", "http://api.bilibili.tv/list?type=xml&tid=3");
			classify_map.put("游戏", "http://api.bilibili.tv/list?type=xml&tid=4");
			classify_map.put("娱乐", "http://api.bilibili.tv/list?type=xml&tid=5");
			classify_map.put("專輯", "http://api.bilibili.tv/list?type=xml&tid=11");
			classify_map.put("新番连载", "http://api.bilibili.tv/list?type=xml&tid=13");
			classify_map.put("專輯三次元", "http://api.bilibili.tv/list?type=xml&tid=15");
			classify_map.put("flash游戏", "http://api.bilibili.tv/list?type=xml&tid=16");
			classify_map.put("游戏视频", "http://api.bilibili.tv/list?type=xml&tid=17");
			classify_map.put("游戏攻略·解说", "http://api.bilibili.tv/list?type=xml&tid=18");
			classify_map.put("Mugen", "http://api.bilibili.tv/list?type=xml&tid=19");
			classify_map.put("舞蹈", "http://api.bilibili.tv/list?type=xml&tid=20");
			classify_map.put("生活娱乐", "http://api.bilibili.tv/list?type=xml&tid=21");
			classify_map.put("三次元鬼畜", "http://api.bilibili.tv/list?type=xml&tid=22");
			classify_map.put("影视", "http://api.bilibili.tv/list?type=xml&tid=23");
			classify_map.put("MAD·AMV", "http://api.bilibili.tv/list?type=xml&tid=24");
			classify_map.put("MMD·3D", "http://api.bilibili.tv/list?type=xml&tid=25");
			classify_map.put("二次元鬼畜", "http://api.bilibili.tv/list?type=xml&tid=26");
			classify_map.put("期刊·其他", "http://api.bilibili.tv/list?type=xml&tid=27");
			classify_map.put("音乐视频", "http://api.bilibili.tv/list?type=xml&tid=28");
			classify_map.put("三次元音乐", "http://api.bilibili.tv/list?type=xml&tid=29");
			classify_map.put("VOCALOID相关", "http://api.bilibili.tv/list?type=xml&tid=30");
			classify_map.put("翻唱", "http://api.bilibili.tv/list?type=xml&tid=31");
			classify_map.put("專輯二次元", "http://api.bilibili.tv/list?type=xml&tid=32");
			classify_map.put("新番二次元", "http://api.bilibili.tv/list?type=xml&tid=33");
			classify_map.put("新番三次元", "http://api.bilibili.tv/list?type=xml&tid=34");
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	//按返回键的时候
	public void onBackPressed() 
	{
		
		if (isClassifyIndex == true) 
		{
			super.onBackPressed();
		} 
		else 
		{
			isClassifyIndex = true;
			isClassifyMode = true;
			loadOrignalData();
		}
		
	}

	
}
